package com.tj.bo.trade.server.service.rule;

import com.fqgj.common.utils.CollectionUtils;
import com.tj.bo.trade.server.entity.bussiness.TradeDailyWinRateEntity;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;

/**
 * 黄金: 每天的胜率都低于55%
 * Author: Amarendar
 * Date: 2020/8/25 下午3:11
 */
@Service
public class TradeGoldRule2 extends BaseTradeRule {

    private static final String RULE_NAME = "每天的胜率都低于55%";

    private static final Integer DEFAULT_PRIORITY = 5;

    @Override
    public void checkRouterRule(RouterContext routerContext, List<RuleResultVO> ruleResultVOList) {

        List<TradeDailyWinRateEntity> tradeDailyWinRateEntityList = binaryTradeDao.selectDailyWinRate(routerContext.getUserCode(), routerContext.getProductCode());
        if (CollectionUtils.isEmpty(tradeDailyWinRateEntityList)) {
            ruleResultVOList.add(new RuleResultVO(routerContext.getProductCode(), RULE_NAME).setMessage("订单数过少1"));
            return;
        }
        Integer totalOrderNum = 0;
        Optional<Integer> optional = tradeDailyWinRateEntityList.stream().map(TradeDailyWinRateEntity::getOrderNum).reduce(Integer::sum);
        if (optional.isPresent()) {
            totalOrderNum = optional.get();
        }

        if (totalOrderNum < redisConfigUtil.getChangeOddsUserTradeNumbers()) {
            ruleResultVOList.add(new RuleResultVO(routerContext.getProductCode(), RULE_NAME).setMessage("订单数过少2"));
            return;
        }

        boolean isOK = true;
        for (TradeDailyWinRateEntity entity : tradeDailyWinRateEntityList) {
            if (entity.getWinRate() == null) {
                continue;
            }
            if (entity.getWinRate().doubleValue() > 0.55) {
                isOK = false;
                break;
            }
        }

        if (isOK) {
            RuleResultVO ruleResultVO = new RuleResultVO(routerContext.getProductCode(), RULE_NAME);
            ruleResultVO.setPriority(DEFAULT_PRIORITY);
            ruleResultVO.setMode(0);
            ruleResultVO.setChangeValue(BigDecimal.ZERO);
            ruleResultVO.setMessage("每天的胜率都低于55%");
            ruleResultVOList.add(ruleResultVO);
        } else {
            ruleResultVOList.add(new RuleResultVO(routerContext.getProductCode(), RULE_NAME).setMessage("每天的胜率都低于55%-条件不满足"));
        }

    }
}
